package com.jky.stmall.front.mapper;

import com.jky.stmall.front.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    //根据分类、name关键字进行查询
    //排序 根据价格、时间、销量、综合（不排序）
    @Select({"<script>",
            "select * from goods",
            "inner join category on goods.category_id = category.id",
            "inner join category as p on category.parent_id = p.id",
            "<where> ",
            "<if test='where.name != null and where.name.length>0'> name like '%${where.name}%'</if>",
            "<if test='where.categoryId != null '> and ",
            "(category_id=#{where.categoryId} or category.parent_id=#{where.categoryId}) ",
            "</if>",
            "and goods.status =0", //商品必须是上架状态
            "and category.status =0 ", //二级分类必须是上架状态
            "and p.status = 0",// 一级分类必须是上架状态
            "</where>",
            "<if test ='orderBy != null and orderBy.length>0'> order by =  ${orderBy} ",
            "</if>",
            "</script>",
    })
    @Results(id = "goods",
            value = {
            //图片
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "pics",
            many = @Many(select = "com.jky.stmall.front.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    List<Goods> select(@Param("where") Goods where, @Param("orderBy") String orderBy);

    //根据商品ID查询
    @Select("select * from goods where id=#{id}")
    @ResultMap("goods")
    Goods selectById(Integer id);

    @Update({
            "update goods",
            "set count=#{count}",
            "where id=#{id}"
    })
    int update(Goods goods);
}